package org.example.atry.myservlet;

import jakarta.servlet.GenericServlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.example.atry.service.agricultuerservicec;
import org.example.atry.service.impl.serviced;
import org.example.atry.util.Users;
import org.json.JSONObject;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
//urlPatterns 就是告诉服务器，当收到某些特定的 URL 请求时，应该将这些请求转发给这个特定的 Servlet 来处理。
//urlPatterns 是 @WebServlet 注解的一个属性，用于定义该 Servlet 能够处理的 URL 模式。当客户端请求的 URL 匹配 urlPatterns 中的任何一个模式时，服务器会将该请求转发给这个 Servlet。
//urlPatterns = {"/pagesome"} 表示这个 Servlet 能够处理所有以 /pagesome 结尾的请求。
//        当客户端访问 http://yourdomain.com/yourapp/pagesome 时，服务器会将这个请求转发给 MyServlet。注意，不是根目录下，用绝对路径比如/进行请求会出错
@WebServlet(urlPatterns = {"/pagesome"})
public class pageaome extends GenericServlet {

    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        // 设置允许跨域的头部信息
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type");
        response.setHeader("Access-Control-Max-Age", "3600");
        agricultuerservicec serviced=new serviced();
        //HttpSession session = request.getSession();

        String radio=request.getParameter("radio");
//        System.out.println(radio);
        /*
        if(session.getAttribute("radio")==null){
         radio=request.getParameter("radio");
            request.setAttribute("radio",radio);

        }else {
          radio = (String) session.getAttribute("radio");
            request.setAttribute("radio",radio);

        }

         */


        int current=1;
        try {
            current = Integer.parseInt(request.getParameter("cu"));
            if (current <= 0) {
                current = 1;
            }
        } catch (Exception e) {
            current=1;
        }
        int pageSize=10;
       /* int totalcounts=serviced.geTotal(radio,current,pageSize);
        Integer totalpages=null;
        if(radio.equals("籼")||radio.equals("粳")){
            totalpages=serviced.getTotal();
        }else {
            totalpages=(int) Math.ceil((double) totalcounts / pageSize);
        }
        */
        Integer totalcounts=null;

        if(radio.equals("籼")||radio.equals("粳")){
            totalcounts=serviced.geTotal(radio);

        }else {
            totalcounts=serviced.getTotal();
        }
        int totalpages=(int) Math.ceil((double)totalcounts/pageSize);

        if(current>=totalpages){
            current=totalpages;
        }
        List<Users> cc=serviced.pagesome(radio,current,pageSize);
        JSONObject as=new JSONObject();
        as.put("cc",cc);
        as.put("totalpages",totalpages);
        as.put("current",current);
        as.put("radio",radio);
        PrintWriter out=response.getWriter();
        out.print(as.toString());
        out.flush();
/*

        request.setAttribute("cc",cc);
        request.setAttribute("current",current);

        request.setAttribute("pageSize",pageSize);

        request.setAttribute("totalpages", totalpages);

        request.getRequestDispatcher("querysomesp.jsp").forward(request, response);

 */
    }
}
